//
//  KNotificationViewController.m
//  Keping
//
//  Created by 柯平 on 2017/10/10.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KNotificationViewController.h"
#import "KSwitchCell.h"
#import "KUser.h"
#import "GPContactRequest.h"
#import "KUserData.h"

@interface KNotificationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic,strong)NSArray* notifications;
@property(nonatomic,strong)NSArray* sectionTitles;
@property (nonatomic, strong) HJVisitFriendInfoModel *visitFriendInfoModel;
@end

@implementation KNotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Notification";
    self.sectionTitles = @[@"All Notifications",@"Message",@"Video Intercom",@"Specific Notification"];
    self.notifications =@[@[@"Enable All Notifications"],@[@"Enable Message",@"Message Vibrate"],@[@"Enable Video Intercom",@"Video Intercom Vibrate"],@[@"Access Card Use",@"Incoming Visiors"]];
    
    self.groupView.refreshEnable = NO;
    self.groupView.loadmoreEnable = NO;
    self.groupView.sectionFooterHeight = CGFLOAT_MIN;
    self.groupView.sectionHeaderHeight = CGFLOAT_MIN;
    
    [self getData];
}

- (void)getData {
    [self showLoading];
    WEAKSELF
    HJVisitFriendInfoRequest *info = [[HJVisitFriendInfoRequest alloc] initWithKey:[KPartyView unarchive].uid];
    [info startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        HJVisitFriendInfoModel *model  = [HJVisitFriendInfoModel modelWithJSON:request.responseData];
        weakSelf.visitFriendInfoModel = model;
        if (model.statusCode == 200) {
            [weakSelf.groupView reloadData];
        } else {
            [weakSelf showErrorText:model.msg errCode:model.statusCode];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showErrorText:request.responseString];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.notifications.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [(NSArray*)self.notifications[section] count];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 50.0;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 0.0001;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 44.0;
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WEAKSELF
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    NSString* title = self.notifications[indexPath.section][indexPath.row];
    KSwitchCell* cell = [tableView dequeueReusableCellWithIdentifier:[KSwitchCell className]];
    if (!cell) {
        cell = [[KSwitchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[KSwitchCell className]];
    }
    cell.titleLabel.text = title;
    if (section == 0) {
        if ([self.visitFriendInfoModel.partyView.enableMessage isEqualToString:@"Y"] && [self.visitFriendInfoModel.partyView.vibrateMessage isEqualToString:@"Y"] && [self.visitFriendInfoModel.partyView.enableVideoIntercom isEqualToString:@"Y"] && [self.visitFriendInfoModel.partyView.vibrateIntercom isEqualToString:@"Y"] && [self.visitFriendInfoModel.partyView.accessCardUse isEqualToString:@"Y"] && [self.visitFriendInfoModel.partyView.incomingVisiors isEqualToString:@"Y"]) {
            cell.switchIb.on = YES;
        } else {
            cell.switchIb.on = NO;
        }
    } else if (section == 1) {
        if (row == 0) {
            cell.switchIb.on = [self.visitFriendInfoModel.partyView.enableMessage isEqualToString:@"Y"] ? YES : NO;
        } else {
            cell.titleLabel.text = @"Vibrate";
            cell.switchIb.on = [self.visitFriendInfoModel.partyView.vibrateMessage isEqualToString:@"Y"] ? YES : NO;
        }
    } else if (section == 2) {
        if (row == 0) {
            cell.switchIb.on = [self.visitFriendInfoModel.partyView.enableVideoIntercom isEqualToString:@"Y"] ? YES : NO;
        } else {
            cell.titleLabel.text = @"Vibrate";
            cell.switchIb.on = [self.visitFriendInfoModel.partyView.vibrateIntercom isEqualToString:@"Y"] ? YES : NO;
        }
    } else if (section == 3) {
        if (row == 0) {
            cell.switchIb.on = [self.visitFriendInfoModel.partyView.accessCardUse isEqualToString:@"Y"] ? YES : NO;
        } else {
            cell.switchIb.on = [self.visitFriendInfoModel.partyView.incomingVisiors isEqualToString:@"Y"] ? YES : NO;
        }
    }
    
    cell.operateBlock = ^(BOOL open) {
        [weakSelf operateWithTitle:title open:open];
    };
    return cell;
}

-(void)operateWithTitle:(NSString*)title open:(BOOL)open{
    if ([@"Enable All Notifications" isEqualToString:title]) {
        // 获取通知中心 是否 允许程序通知消息的值。
        NSString *isOn = open ? @"Y" : @"N";
        [self upDateForAllUserMessagea:@{
                                         @"enableAllNotifications":isOn,
                                         @"enableMessage":isOn,
                                         @"vibrateMessage":isOn,
                                         @"enableVideoIntercom":isOn,
                                         @"vibrateIntercom":isOn,
                                         @"accessCardUse":isOn,
                                         @"incomingVisiors":isOn
                                         }];
    }else if ([@"Enable Message" isEqualToString:title]){
        [self upDateForUserMessage:@"enableMessage" isOpen:open];
    }else if ([@"Message Vibrate" isEqualToString:title]){
        [self upDateForUserMessage:@"vibrateMessage" isOpen:open];
    } else if ([@"Enable Video Intercom" isEqualToString:title]){
        [self upDateForUserMessage:@"enableVideoIntercom" isOpen:open];
    } else if ([@"Video Intercom Vibrate" isEqualToString:title]){
        [self upDateForUserMessage:@"vibrateIntercom" isOpen:open];
    } else if ([@"Access Card Use" isEqualToString:title]){
        [self upDateForUserMessage:@"accessCardUse" isOpen:open];
    } else if ([@"Incoming Visiors" isEqualToString:title]){
        [self upDateForUserMessage:@"incomingVisiors" isOpen:open];
    }
}

- (void)upDateForAllUserMessagea:(NSDictionary *)allUser {
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:KUpdateprofileURL];
    request.isPost = YES;
    request.params = allUser;
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        NSDictionary *response = request.responseJSONObject;
        if ([response[@"statusCode"] integerValue] != 200) {
            [self showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
            return;
        }
        [weakSelf getData];
        [weakSelf showSuccess:@"success"];
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showErrorText:@"failture"];
    }];
}

- (void)upDateForUserMessage:(NSString *)name isOpen:(BOOL)open {
    
    NSString *isOn = open ? @"Y" : @"N";
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:KUpdateprofileURL];
    request.isPost = YES;
    request.params = @{name:isOn};
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        NSDictionary *response = request.responseJSONObject;
        if ([response[@"statusCode"] integerValue] != 200) {
            [self showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
            return;
        }
        [weakSelf getData];
        [weakSelf showSuccess:@"success"];
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showErrorText:@"failture"];
    }];
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view = [UIView new];
    UILabel* titleLabel = [UILabel new];
    titleLabel.font = SystemFont;
    [titleLabel setFrame:CGRectMake(10, 20, kScreenWidth-20, 20)];
    titleLabel.text = self.sectionTitles[section];
    [view addSubview:titleLabel];
    return view;
}

//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return self.sectionTitles[section];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
